package com.jcsz.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.jcsz.gulimall.product.entity.AttrEntity;
import com.jcsz.gulimall.product.service.AttrAttrgroupRelationService;
import com.jcsz.gulimall.product.service.CategoryService;
import com.jcsz.gulimall.product.vo.AttrGroupRelationVo;
import com.jcsz.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.jcsz.gulimall.product.entity.AttrGroupEntity;
import com.jcsz.gulimall.product.service.AttrGroupService;
import com.jcsz.common.utils.PageUtils;
import com.jcsz.common.utils.R;


/**
 * 属性分组
 *
 * @author 一把Po吉他
 * @email volibear@gulimall.com
 * @date 2022-10-16 08:40:06
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    CategoryService categoryService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;


    /**
     * 获取分类下所有分组&关联属性
     * /product/attrgroup/{catelogId}/withattr
     **/
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrByCategoryId(@PathVariable Long catelogId) {
        List<AttrGroupWithAttrsVo> attrGroupWithAttrsVos = attrGroupService.getAttrGroupWithAttrByCategoryId(catelogId);
        return R.ok().put("data", attrGroupWithAttrsVos);
    }


    /**
     * 移除属性与分组关联关系
     * /product/attrgroup/attr/relation/delete
     */
    @RequestMapping("/attr/relation/delete")
    public R deleteAttrRelation(@RequestBody List<AttrGroupRelationVo> attrGroupRelationVos) {
        attrAttrgroupRelationService.deleteBatch(attrGroupRelationVos);

        return R.ok();
    }

    /**
     * 添加属性与分组关联关系
     * /product/attrgroup/attr/relation
     */
    @RequestMapping("/attr/relation")
    public R saveAttrRelation(@RequestBody List<AttrGroupRelationVo> attrGroupRelationVos) {
        attrAttrgroupRelationService.saveBatch(attrGroupRelationVos);

        return R.ok();
    }

    /**
     * 属性分组关联属性列表
     * //product/attrgroup/{attrgroupId}/attr/relation
     */
    @RequestMapping("/{attrgroupId}/attr/relation")
    //@RequiresPermissions("product:attrgroup:list")
    public R getAttrRelation(@PathVariable("attrgroupId") Long attrgroupId) {
        List<AttrEntity> attrEntities = attrGroupService.queryAttrs(attrgroupId);

        return R.ok().put("data", attrEntities);
    }

    /**
     * 属性分组关联属性列表
     * //product/attrgroup/{attrgroupId}/attr/relation
     */
    @RequestMapping("/{attrgroupId}/noattr/relation")
    //@RequiresPermissions("product:attrgroup:list")
    public R getNoAttrRelation(@RequestParam Map<String, Object> params, @PathVariable("attrgroupId") Long attrgroupId) {
        PageUtils page = attrGroupService.queryNoAttrs(params, attrgroupId);
        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }

    @RequestMapping("/list/{catId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R listByCatId(@RequestParam Map<String, Object> params, @PathVariable Long catId) {
        PageUtils page = attrGroupService.queryPageByCatId(params, catId);
        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long[] catelogPath = categoryService.getCatelogPathById(attrGroup.getCatelogId());
        attrGroup.setCatelogPath(catelogPath);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));
        return R.ok();
    }

}
